package com.renjian.funplaypro.controllers;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.renjian.funplaypro.model.User;
import com.renjian.funplaypro.services.UserService;
import com.renjian.funplaypro.utils.CommonResult;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@CrossOrigin
public class UserController {
    @Resource
    UserService userService;
    /**
     * @author renjian
     * @description 用户注册
     * @param user
     * @return
     */
    @PostMapping("/user/register")
    public CommonResult register(@RequestBody User user){
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.eq("email",user.getUsermail());
        User u = userService.getOne(wrapper);
        if(u!=null){
            return new CommonResult().failed("该用户名或邮箱已经被注册，请更改!");
        }else{
            user.setSecurePassword();
            userService.save(user);
        }
        return new CommonResult().success(user);
    }


    /**
     * @author renjian
     * @description 用户登录
     * @param user
     * @return
     */
    @PostMapping("/user/login")
    public CommonResult login(@RequestBody User user){
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.eq("email",user.getUsermail());
        user.setSecurePassword();
        wrapper.eq("password",user.getPassword());
        User u = userService.getOne(wrapper);
        if(u==null){
            return new CommonResult().failed("用户名或密码错误，请重新登录!");
        }
        return new CommonResult().success(u);
    }
}
